/*
 * Copyright (c) 2008 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.stripesbook.article.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.StrictBinding;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import net.sourceforge.stripes.validation.ValidationErrors;
import net.sourceforge.stripes.validation.ValidationMethod;

import org.stripesbook.article.model.Database;
import org.stripesbook.article.model.User;
import org.stripesbook.article.view.ExampleForm;
import org.stripesbook.rayures.action.RayuresBaseActionBean;
import org.stripesbook.rayures.view.stripes.SForm;
import org.stripesbook.rayures.view.stripes.SSubmit;
import org.stripesbook.rayures.view.stripes.SText;

@UrlBinding("/activate")
@StrictBinding
public class ActivateActionBean extends RayuresBaseActionBean {
  @ValidateNestedProperties({
    @Validate(field="username", required=true),
    @Validate(field="activationCode", required=true)
  })
  private User user;

  /* Getters and setters for user */
  public User getUser() {
    return user;
  }
  public void setUser(User user) {
    this.user = user;
  }

  @DefaultHandler
  @DontValidate
  public void view() {
  }

  public SForm getForm() {
    return tag(ExampleForm.class).beanclass(ActivateActionBean.class)
      .addInput(tag(SText.class).name("user.username"), "Username")
      .addInput(tag(SText.class).name("user.activationCode"), "Activation code")
      .addButton(tag(SSubmit.class).name("activate").value("Activate"));
  }

  @ValidationMethod
  public void validateUser(ValidationErrors errors) {
    if (!Database.userExists(user.getUsername())) {
      errors.add("user.username",
        new SimpleError("The username is not valid"));
    }
    else {
      User existingUser = Database.getUser(user.getUsername());
      if (!existingUser.getActivationCode().equals(
         user.getActivationCode().toUpperCase()))
      {
        errors.add("user.activationCode",
          new SimpleError("The activation code is not valid"));
      }
    }
  }

  public Resolution activate() {
    Database.getUser(user.getUsername()).setActivated(true);
    getContext().getMessages().add(
      new SimpleMessage("Your account has been activated!"));
    return new RedirectResolution(LoginActionBean.class);
  }
}
